package com.example.jiaju.ui;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.jiaju.DataApplication;
import com.example.jiaju.R;
import com.example.jiaju.adapter.BBSAdapter;
import com.example.jiaju.adapter.FurnitureInspirationAdapter;
import com.example.jiaju.adapter.FurnitureThemeAdapter;
import com.example.jiaju.api.BBSApi;
import com.example.jiaju.entity.BBS;
import com.example.jiaju.entity.FurnitureInspiration;
import com.example.jiaju.entity.FurnitureTheme;
import com.example.jiaju.utils.MyItemDecoration;
import com.example.jiaju.utils.RetrofitUtils;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * 论坛-家具灵感
 */

public class FurnitureFragment extends Fragment {
    private RecyclerView furnitureRecyclerView;
    private FurnitureThemeAdapter furnitureThemeAdapter;
    private List<FurnitureTheme> furnitureThemeList=new ArrayList<>();

    private RecyclerView furnitureInspirationRecyclerView;
    private FurnitureInspirationAdapter furnitureInspirationAdapter;
    private List<BBS.PageDTO> furnitureInspirationList=new ArrayList<>();
    public FurnitureFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_furniture, container, false);

        FurnitureTheme furnitureTheme=
                new FurnitureTheme("https://starry-lixu.oss-cn-hangzhou.aliyuncs.com/202212111652771.png","双十一好物");
        furnitureThemeList.add(furnitureTheme);
        FurnitureTheme furnitureTheme1=
                new FurnitureTheme("https://starry-lixu.oss-cn-hangzhou.aliyuncs.com/202212111652771.png","家居改造日记");
        furnitureThemeList.add(furnitureTheme1);
        FurnitureTheme furnitureTheme2=
                new FurnitureTheme("https://starry-lixu.oss-cn-hangzhou.aliyuncs.com/202212111652771.png","孩子书房收纳");
        furnitureThemeList.add(furnitureTheme2);

        furnitureRecyclerView=view.findViewById(R.id.furniture_theme_recyclerview);
        furnitureThemeAdapter=new FurnitureThemeAdapter(furnitureThemeList,getActivity());
        furnitureRecyclerView.setAdapter(furnitureThemeAdapter);
        furnitureRecyclerView.addItemDecoration(new MyItemDecoration(8));
        furnitureRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(),
                LinearLayoutManager.HORIZONTAL,false));



        //用户吐槽 家具的BBS
        BBSApi api= RetrofitUtils.getRetrofit(DataApplication.HTTP_URL).create(BBSApi.class);
        Call<BBS> call=api.getBBSByKind(31,1,5);
        call.enqueue(new Callback<BBS>() {
            @Override
            public void onResponse(Call<BBS> call, Response<BBS> response) {
                BBS bbs=response.body();
                if(bbs==null){
                    Toast.makeText(getContext(),"家具灵感数据请求异常！",Toast.LENGTH_SHORT).show();
                }else{
                    furnitureInspirationList=bbs.page;
                    //填充吐槽信息
                    furnitureInspirationRecyclerView=view.findViewById(R.id.furniture_theme_main_recyclerview);
                    furnitureInspirationAdapter=new FurnitureInspirationAdapter(furnitureInspirationList);
                    furnitureInspirationRecyclerView.setAdapter(furnitureInspirationAdapter);
                    furnitureInspirationRecyclerView.addItemDecoration(new MyItemDecoration(8));
                    furnitureInspirationRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,
                            StaggeredGridLayoutManager.VERTICAL));
                }

            }

            @Override
            public void onFailure(Call<BBS> call, Throwable t) {

            }
        });

        return view;
    }
}